/**
 * @file main.c
 * @author 百问网开发团队
 * @brief 
 * @version 0.1
 * @date 2022-05-30
 * 
 * @copyright Copyright (c) 2022 深圳百问网科技有限公司
 * 
 */

#include "drv_sysclk.h"
#include "drv_systick.h"
#include "drv_uart.h"
#include "drv_sdio.h"
#include "mm32f327xx_hal_config.h"

#include <stdio.h>

sdcard_t app_sdcard;
uint8_t app_sdcard_read_buf[SDCARD_BLOCK_SIZE*2];
uint8_t app_sdcard_write_buf[SDCARD_BLOCK_SIZE*2];

uint8_t app_test_dat8[4] = {0x11, 0x22, 0x33, 0x44};

uint32_t app_sdcard_write_blk_idx = 0u;
uint32_t app_sdcard_read_blk_idx = 0u;
uint8_t  app_sdcard_write_seed = 0u;

/**
 * @brief ARM处理器程序入口函数
 * 
 * @return int 通常main函数不会有返回值
 */
int main(void)
{
    uint8_t c;
    uint32_t tmp32;
    /* 初始化系统时钟频率为120MHz */
    SystemClockConfig();
    /* 配置滴答定时器的技术周期是1kHz */
    Drv_SysTick_Config();
    /* 初始化串口 */
    Drv_UART_Init();

    /* 初始化SDIO */
    Drv_SDIO_Init(&app_sdcard, SDIO);
    printf("sdcard_init() done.\r\n");

    while(1)
    {
        /* TDO Your Task */
        c = getchar();
        switch (c)
        {
            case 'a': /* read single block. */
                printf("a: read single block.\r\n");

                printf("reading block num: %u\r\n", (unsigned)app_sdcard_read_blk_idx);
                Drv_SDIO_Read_SingleBlock(&app_sdcard, app_sdcard_read_blk_idx++, app_sdcard_read_buf);
                for (uint32_t i = 0u; i < SDCARD_BLOCK_SIZE; i++)
                {
                    if ( (i % 16) == 0u)
                    {
                        printf("\r\n");
                    }
                    printf("0x%2X, ", app_sdcard_read_buf[i]);
                }

                printf("\r\n\r\n");
            
                break;

            case 'b': /* read multiple blocks. */
                printf("b: sdcard_read_multi_blocks().\r\n");

                printf("reading block num: %u, %u\r\n", (unsigned)app_sdcard_read_blk_idx, (unsigned)app_sdcard_read_blk_idx+1u);
                Drv_SDIO_Read_MultiBlocks(&app_sdcard, app_sdcard_read_blk_idx, 2u, app_sdcard_read_buf);
                app_sdcard_read_blk_idx += 2u;
                for (uint32_t i = 0u; i < SDCARD_BLOCK_SIZE*2u; i++)
                {
                    if ( (i % 16) == 0u)
                    {
                        printf("\r\n");
                    }
                    printf("0x%2X, ", app_sdcard_read_buf[i]);
                }
                printf("\r\n\r\n");

                break;

            case 'c': /* write single block. */
                printf("c: sdcard_write_single_block.\r\n");

                printf("writing block num: %u\r\n", (unsigned)app_sdcard_write_blk_idx);
                tmp32 = app_sdcard_write_seed;
                for (uint32_t i = 0u; i < SDCARD_BLOCK_SIZE; i++)
                {
                    app_sdcard_write_buf[i] = (tmp32++) % 256;
                }

                Drv_SDIO_Write_SingleBlock(&app_sdcard, app_sdcard_write_blk_idx++, app_sdcard_write_buf);
                printf("write done.\r\n\r\n");

                break;

            case 'd': /* write multiple blocks. */
                printf("d: sdcard_write_multi_blocks().\r\n");

                printf("writing block num: %u, %u\r\n", (unsigned)app_sdcard_write_blk_idx, (unsigned)app_sdcard_write_blk_idx+1);
                tmp32 = app_sdcard_write_seed;
                for (uint32_t i = 0u; i < SDCARD_BLOCK_SIZE*2u; i++)
                {
                    app_sdcard_write_buf[i] = (tmp32++) % 256;
                }

                Drv_SDIO_Write_MultiBlocks(&app_sdcard, app_sdcard_write_blk_idx, 2u, app_sdcard_write_buf);
                app_sdcard_write_blk_idx += 2u;

                break;

            case 'e':
                printf("e: sdcard_init().\r\n");
                Drv_SDIO_Init(&app_sdcard, SDIO);
                printf("done.\r\n");
                break;            

            case 'f':
                printf("f: reset the read/write index.\r\n");
                app_sdcard_write_blk_idx = 0u;
                app_sdcard_read_blk_idx = 0u;
                break;

            case 'g':
                printf("g: increase write seed.\r\n");
                app_sdcard_write_seed++;
                printf("app_sdcard_write_seed = %u\r\n", (unsigned)app_sdcard_write_seed);
                break;

            case 'h':
                printf("h: decrease write seed.\r\n");
                app_sdcard_write_seed--;
                printf("app_sdcard_write_seed = %u\r\n", (unsigned)app_sdcard_write_seed);
                break;

            default:
                printf("\r\n");
            
                printf("a: sdcard_read_single_block().\r\n");
                printf("b: sdcard_read_multi_blocks().\r\n");
                printf("c: sdcard_write_single_block().\r\n");
                printf("d: sdcard_write_multi_blocks().\r\n");
                printf("e: sdcard_init().\r\n");
                printf("f: reset the read/write index.\r\n");
                printf("g: increase write seed.\r\n");
                printf("h: decrease write seed.\r\n");
                
                printf("\r\n");
                break;
        }
    }
}


